import { NextRequest } from 'next/server'
import prisma from '@/lib/prisma'

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams
    const parentCode = searchParams.get('parentCode')

    const regions = await prisma.region.findMany({
      where: {
        parentCode: parentCode || null  // 如果 parentCode 为空，则查询顶级节点
      },
      orderBy: {
        orderNum: 'asc'
      }
    })

    return Response.json(regions)
  } catch (error) {
    console.error('获取行政区划失败:', error)
    return Response.json({ error: '获取行政区划失败' }, { status: 500 })
  }
} 